﻿using DZParallelLib;
using System;
using System.IO;
using System.Linq;

namespace RenderLargeImageCollection
{
    class Program
    {
        /// <summary>
        /// Stand alone program to render a large image collection. We mostly are running this so that
        /// we can do some memory (and the like) tests.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //
            // Various directories.
            //

            var imageBaseDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "LargeImageDirectory"));
            if (!imageBaseDir.Exists)
            {
                Console.WriteLine("Please run MakeLargeImageCollection <n> before running this to generate the input data!");
                return;
            }
            var imageSCDir = new DirectoryInfo(Path.Combine(imageBaseDir.FullName, "sc"));

            //
            // Clean up anything we need to from the last run.
            //

            var output = new FileInfo(Path.Combine(imageBaseDir.FullName, "dz.xml"));
            if (output.Exists)
            {
                Console.WriteLine("Deleting old output dz.xml");
                output.Delete();
            }
            var renderDB = new FileInfo(Path.Combine(imageBaseDir.FullName, "DZRenderedImageDbXML.rendered_info"));
            if (renderDB.Exists)
            {
                Console.WriteLine("Deleting old rendering database");
                renderDB.Delete();
            }

            // 
            // Do the render...
            //

            var c = new RenderDZCollectionFromImages();
            var tstart = DateTime.Now;
            var r = c.RenderCollection(imageSCDir.EnumerateFiles(),
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output
                );

            //
            // Wait for it to finish!
            //

            r.Wait();
            var tfinish = DateTime.Now;

            Console.WriteLine("Rendering {0} images took {1}", imageSCDir.EnumerateFiles().Count(), tfinish - tstart);
        }
    }
}
